package com.hyz.assetManager.service.impl;

import com.hyz.assetManager.mapper.ApprovalLogMapper;
import com.hyz.assetManager.mapper.ApprovalMapper;
import com.hyz.assetManager.mapper.AssetInboundMapper;
import com.hyz.assetManager.pojo.Approval;
import com.hyz.assetManager.pojo.ApprovalLog;
import com.hyz.assetManager.pojo.AssetInbound;
import com.hyz.assetManager.pojo.myEnum.ApprovalClassEnum;
import com.hyz.assetManager.pojo.myEnum.AssetInboundStatusEnum;
import com.hyz.assetManager.service.AssetManagerService;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.permissions.assetManager.AssetInboundRequest;
import pojo.permissions.assetManager.AssetInboundStatusResponse;
import util.GetValue;

import java.sql.Date;
import java.util.List;

/**
 * @author HGD
 * @date 2022/9/30 13:58
 */
@Service
public class AssetManagerServiceImpl implements AssetManagerService {
    @Autowired
    private ApprovalMapper approvalMapper;
    @Autowired
    private ApprovalLogMapper approvalLogMapper;
    @Autowired
    private AssetInboundMapper assetInboundMapper;

    @Override
    public void createAssetInbound(AssetInboundRequest assetInboundRequest) {
        LoggerFactory.getLogger(AssetManagerServiceImpl.class).info("createAssetInbound 被调用");
        // TODO: 传入的数据变成分库申请。申请在数据库中的保存路径是：approval——> approvalLog——>assetInbound
        Long id = assetInboundRequest.getId();
        List<Long> assetIdList = assetInboundRequest.getAssetIds();
        Long assetManagerId = assetInboundRequest.getAssetManagerId();
        Long repositoryId = assetInboundRequest.getRepositoryId();

        AssetInboundStatusEnum create = AssetInboundStatusEnum.CREATE;

        Approval approval = Approval.builder()
                .id(new GetValue().getUuid())
                .applicationId(id)
                .applicationClass(ApprovalClassEnum.ASSET_INBOUND.getKey())
                .status(create.getKey()).build();
        ApprovalLog approvalLog = ApprovalLog.builder()
                .id(new GetValue().getUuid())
                .approvalId(approval.getId())
                .approverId(assetManagerId)
                .approvalDate(new Date(new java.util.Date().getTime()))
                .result(create.getValue()).build();
        approvalLogMapper.createAssetInboundApprovalLog(approvalLog);
        approvalMapper.createAssetInboundApproval(approval);
        for (Long aLong : assetIdList) {
            // 遍历列表
            AssetInbound assetInbound
                    = AssetInbound.builder()
                    .id(id)
                    .assetId(aLong)
                    .repositoryId(repositoryId)
                    .assetManagerId(assetManagerId).build();
            assetInboundMapper.createAssetInbound(assetInbound);
        }
    }

    @Override
    public List<AssetInboundStatusResponse> getAssetInboundStatus(Long assetManagerId) {
        return assetInboundMapper.getAssetInboundStatus(assetManagerId);
    }

}
